<!DOCTYPE html>
<html lang="zh-CN" class="loading">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>创建自己的树莓派Pico-C语言项目 - Skyone-Blog</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="树莓派,树莓派Pico,单片机,"> 
    <meta name="description" content="Skyone技术博客,本文详细介绍了如何在Windows下创建自己的树莓派Pico项目。虽然遇到一些坑，但还是成功了，分享一下这个过程。"> 
    <meta name="author" content="王志伟"> 
    <link rel="alternative" href="atom.xml" title="Skyone-Blog" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
<link rel="stylesheet" href="/css/diaspora.css">

    <!-- 看板娘
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css">
    <script src="/live2d-widget/autoload.js"></script>
    -->
    <!-- MD5 -->
    <script src="/js/md5.min.js"></script>
    <!-- MathJax -->
    
        <script type="text/x-mathjax-config">
        (function () {
            let target = document.getElementsByClassName("content")[0];
            MathJax.Hub.Config({
                tex2jax: {
                    inlineMath:  [ ["$", "$"] ],
                    displayMath: [ ["$$","$$"] ],
                    skipTags: ['script', 'noscript', 'style', 'textarea', 'pre','code','a'],
                    ignoreClass:"class1"
                }
            });
            MathJax.Hub.Queue(["Typeset",MathJax.Hub,target]);
        })();
    </script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

    
<meta name="generator" content="Hexo 5.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">Skyone-Blog</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://www.skyone.host"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">创建自己的树莓派Pico-C语言项目</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">创建自己的树莓派Pico-C语言项目</h1>
        <div class="stuff">
            <span>四月 11, 2021</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/" rel="tag">环境配置</a></li></ul>


        </div>
        <div class="content markdown">
            <p>继<a href="https://www.skyone.host/2021/02/24/win10-xia-pei-zhi-shu-mei-pai-pico-de-micropython-yu-c-huan-jing/">Win10下配置树莓派Pico的MicroPython与C环境</a>后的又一篇介绍树莓派Pico的文章</p>
<p>刚配置完环境就发现网上的教程全是教你使用树莓派官方的示例项目，然而几乎没有关于创建自己项目的教程，既然如此，我来吧</p>
<p>经过不懈的努力<del>实际上是忘了弄。。</del>，终于完成了树莓派Pico的C/C++项目的创建，对，你没听错，树莓派Pico C/C++项目的创建相对于minpython或Arduino来说是非常难的，尤其是在Windows环境下还有几个令人绝望的坑。废话不多说，开始这篇文章。</p>
<h2 id="写在前面"><a href="#写在前面" class="headerlink" title="写在前面"></a>写在前面</h2><p>这篇文章<strong>仅适用于Windows系统</strong>，要求</p>
<ul>
<li>安装了Python3且已经加入环境变量</li>
<li>安装并配置完成了Git</li>
<li>安装了CMake并加入了环境变量</li>
<li>安装了graphviz并加入了环境变量（重点）</li>
<li>安装了mingw64并加入了环境变量</li>
<li>安装了gcc-arm-none-eabi</li>
<li>VSCode或者CLion（推荐后者）</li>
</ul>
<h2 id="clone树莓派官方仓库"><a href="#clone树莓派官方仓库" class="headerlink" title="clone树莓派官方仓库"></a>clone树莓派官方仓库</h2><p>需要clone两个仓库，都在GitHub上</p>
<p>CMD切换到一个你觉得合适的文件夹，记住文件夹目录，输入命令</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git clone git@github.com:raspberrypi/pico-sdk.git</span><br><span class="line">git clone git@github.com:raspberrypi/pico-project-generator.git</span><br></pre></td></tr></table></figure>
<p>由于众所周知的原因，最近GitHub非常不稳定，不过使用SSH克隆没多大问题</p>
<h2 id="创建C-C-项目"><a href="#创建C-C-项目" class="headerlink" title="创建C/C++项目"></a>创建C/C++项目</h2><p>树莓派Pico的官方SDK使用CMake作为构建系统，且项目配置特别复杂，官方非常贴心的提供了Python写的可以选择GUI界面的构建器，赞👍</p>
<p>首先打开命令行，进入刚刚Git克隆的<strong>pico-project-generator</strong>目录，尝试运行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python ./pico_project.py --gui</span><br></pre></td></tr></table></figure>
<p>当然，运行失败。。。</p>
<p>显示未配置<code>PICO_SDK_PATH</code>，好，这个好解决，一下两种方法任选其一</p>
<ul>
<li><strong>方法一：设置临时环境变量</strong></li>
</ul>
<p>这种方法关闭powershell后环境变量就失效了</p>
<p>使用<strong>powershell</strong>打开<strong>pico-project-generator</strong>目录运行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash">env:PICO_SDK_PATH=你克隆的pico-sdk目录</span></span><br><span class="line"><span class="meta">$</span><span class="bash">env:PICO_TOOLCHAIN_PATH=你安装的gcc-arm-none-eabi目录/bin</span></span><br><span class="line">python ./pico_project.py --gui</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>方法二：设置永久环境变量</strong></li>
</ul>
<p>很多人在更新win10 20H2后找不到环境变量在哪设置，我姑且再提一下</p>
<p>打开win10的设置–&gt;系统–&gt;关于–&gt;高级系统设置–&gt;环境变量，按如下方法配置</p>
<img src="https://tc.skyone.host/blog/post/%E5%88%9B%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E6%A0%91%E8%8E%93%E6%B4%BEPico-C%E8%AF%AD%E8%A8%80%E9%A1%B9%E7%9B%AE/%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F.png" alt="环境变量" style="zoom: 67%;" />

<p><strong>重新打开cmd或powershell</strong></p>
<p>再次运行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python ./pico_project.py --gui</span><br></pre></td></tr></table></figure>
<p>OK，不论你用哪种方法，现在应该看到如下界面：</p>
<img src="https://tc.skyone.host/blog/post/%E5%88%9B%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E6%A0%91%E8%8E%93%E6%B4%BEPico-C%E8%AF%AD%E8%A8%80%E9%A1%B9%E7%9B%AE/%E9%A1%B9%E7%9B%AE%E7%94%9F%E6%88%90%E5%99%A8.png" alt="项目生成器" style="zoom:67%;" />

<p>嗯，也不是很难嘛，不过你以为这就完了？大坑还在后面！</p>
<p>随意填一下这下表单，因为你不可能创建成功。。。</p>
<p>点击OK，弹出一个窗口，然而，什么都没出现。一看命令行，不对劲，出现了一堆错误，最后显示找不到文件。</p>
<p><strong>重点来了！</strong></p>
<p>如果你不是刚刚安装graphviz的，请卸载重装</p>
<p>然后，打开<code>Python安装目录/Lib/subprocess.py</code></p>
<p>将<code>Popen</code>类的构造函数的参数的<code>shell=False</code>改为<code>shell=True</code>，如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, args, bufsize=-<span class="number">1</span>, executable=<span class="literal">None</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">             stdin=<span class="literal">None</span>, stdout=<span class="literal">None</span>, stderr=<span class="literal">None</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">             preexec_fn=<span class="literal">None</span>, close_fds=<span class="literal">True</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">             shell=<span class="literal">True</span>, cwd=<span class="literal">None</span>, env=<span class="literal">None</span>, universal_newlines=<span class="literal">None</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">             startupinfo=<span class="literal">None</span>, creationflags=<span class="number">0</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">             restore_signals=<span class="literal">True</span>, start_new_session=<span class="literal">False</span>,</span></span></span><br><span class="line"><span class="function"><span class="params">             pass_fds=(<span class="params"></span>), *, encoding=<span class="literal">None</span>, errors=<span class="literal">None</span>, text=<span class="literal">None</span></span>):</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><strong>介绍一下原因</strong></p>
<p>该类在运行时需要调用<code>echo</code>命令，这在linux下当然没问题，因为linux下<code>echo</code>是一个可执行文件，而Windows下则是一个命令行内置的命令，由此导致找不到文件，我们通过更改源代码，让程序打开一个shell再运行echo就没问题了。</p>
</blockquote>
<p>OK，这下终于完成了</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python ./pico_project.py --gui</span><br></pre></td></tr></table></figure>
<p>关于下图怎么配置，不在本篇文章的介绍范围内<del>实际上是有些我也不懂。。</del></p>
<img src="https://tc.skyone.host/blog/post/%E5%88%9B%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E6%A0%91%E8%8E%93%E6%B4%BEPico-C%E8%AF%AD%E8%A8%80%E9%A1%B9%E7%9B%AE/%E9%A1%B9%E7%9B%AE%E7%94%9F%E6%88%90%E5%99%A8.png" alt="项目生成器" style="zoom:67%;" />

<p>配置好选项后，依旧点击OK，这次没有报错了，虽然我操作时弹出的窗口里面还是乱码，但项目已经创建成功了</p>
<h2 id="开始开发吧"><a href="#开始开发吧" class="headerlink" title="开始开发吧"></a>开始开发吧</h2><p>快使用VSCode或CLion开始开发你的树莓派Pico C/C++项目吧~~~</p>
<p>开发环境没配置好的请看<a href="https://www.skyone.host/2021/02/24/win10-xia-pei-zhi-shu-mei-pai-pico-de-micropython-yu-c-huan-jing/">Win10下配置树莓派Pico的MicroPython与C环境</a></p>
<p><strong>特别注意！配载永久环境变量后CLion不需要再配置项目，直接打开文件夹就可以开发了</strong></p>
<img src="https://tc.skyone.host/blog/post/%E5%88%9B%E5%BB%BA%E8%87%AA%E5%B7%B1%E7%9A%84%E6%A0%91%E8%8E%93%E6%B4%BEPico-C%E8%AF%AD%E8%A8%80%E9%A1%B9%E7%9B%AE/%E4%BE%8B%E5%9B%BE.png" alt="例图" style="zoom: 67%;" />


            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title='0' data-url='https://tc.skyone.host/blog/music/%E6%B4%9B%E5%A4%A9%E4%BE%9D%20-%20%E5%A4%9C%E8%88%AA%E6%98%9F.flac'></li>
                        
                    
                        
                            <li title='1' data-url='https://tc.skyone.host/blog/music/%E6%B4%9B%E5%A4%A9%E4%BE%9D%20-%20%E8%8C%89%E8%8E%89%E8%8A%B1.mp3'></li>
                        
                    
                        
                            <li title='2' data-url='https://tc.skyone.host/blog/music/%E8%B5%A4%E7%BE%BD%20-%20%E4%B8%87%E5%8F%A4%E7%94%9F%E9%A6%99.flac'></li>
                        
                    
                        
                            <li title='3' data-url='https://tc.skyone.host/blog/music/%E6%B4%9B%E5%A4%A9%E4%BE%9D%20-%20%E4%B8%BA%E8%B0%81%E8%80%8C%E4%B8%BA.mp3'></li>
                        
                    
                        
                            <li title='4' data-url='https://tc.skyone.host/blog/music/%E4%B9%90%E6%AD%A3%E7%BB%AB%20-%20Faded.mp3'></li>
                        
                    
                        
                            <li title='5' data-url='https://tc.skyone.host/blog/music/bilibili2018%20-%20%E8%87%B3%E5%B0%8A%E9%A9%AC%E7%94%B2.flac'></li>
                        
                    
                </ul>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='true'
        data-ae='true'
        data-ci='d5e197ef955b7f3268e5'
        data-cs='7d4feed7a179ad28943b0865b7970814073145ad'
        data-r='blog-gitalk'
        data-o='skyone-wzw'
        data-a='skyone-wzw'
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
			<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%99%E5%9C%A8%E5%89%8D%E9%9D%A2"><span class="toc-number">1.</span> <span class="toc-text">写在前面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#clone%E6%A0%91%E8%8E%93%E6%B4%BE%E5%AE%98%E6%96%B9%E4%BB%93%E5%BA%93"><span class="toc-number">2.</span> <span class="toc-text">clone树莓派官方仓库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BAC-C-%E9%A1%B9%E7%9B%AE"><span class="toc-number">3.</span> <span class="toc-text">创建C&#x2F;C++项目</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%80%E5%A7%8B%E5%BC%80%E5%8F%91%E5%90%A7"><span class="toc-number">4.</span> <span class="toc-text">开始开发吧</span></a></li></ol>	
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/skyone.min.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>




</html>
